<!--********************************************************************
* Copyright© 2000 - 2018 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title data-i18n="resources.title_editFeature"></title>
    <style>
        .editPane {
            position: absolute;
            right: 10px;
            top: 10px;
            text-align: center;
            background: #FFF;
            z-index: 1000;
        }
    </style>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
<div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
<div>
    <div class="panel panel-primary editPane" id="editPane">
        <div class='panel-heading'>
            <h5 class='panel-title text-center' data-i18n="resources.text_editSingle"></h5></div>
        <div class='panel-body content'>
            <input type='button' class='btn btn-default' data-i18n="[value]resources.btn_addMarker" onclick='addMarker()'/>&nbsp;
            <input type='button' class='btn btn-default' data-i18n="[value]resources.btn_undoAdd" onclick='revocationMarker()'/>
            <input type='button' class='btn btn-default' data-i18n="[value]resources.text_input_value_submit" onclick='commit()'/>&nbsp;
            <input type='button' class='btn btn-default' data-i18n="[value]resources.text_input_value_clear" onclick='clearLayer()'/>
        </div>
    </div>
</div>
<script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
<script type="text/javascript" src="../../dist/mapboxgl/include-mapboxgl.js"></script>
<script>
    var map, id = [], pointFeature, alertDiv, featureService,
        baseUrl = (window.isLocal ? window.server : "http://support.supermap.com.cn:8090") + "/iserver/services/map-world/rest/maps/World",
        mapUrl = baseUrl + "/zxyTileImage.png?z={z}&x={x}&y={y}",
        dataUrl = (window.isLocal ? window.server : "http://support.supermap.com.cn:8090") + "/iserver/services/data-world/rest/data";
    var attribution = "<a href='https://www.mapbox.com/about/maps/' target='_blank'>© Mapbox </a>" +
        " with <span>© <a href='http://iclient.supermap.io' target='_blank'>SuperMap iClient</a> | </span>" +
       " Map Data <span>© <a href='http://support.supermap.com.cn/product/iServer.aspx' target='_blank'>SuperMap iServer</a></span> ";

    map = new mapboxgl.Map({
        container: 'map',
        style: {
            "version": 8,
            "sources": {
                "raster-tiles": {
                    "attribution": attribution,
                    "type": "raster",
                    "tiles": [mapUrl],
                    "tileSize": 256
                }
            },
            "layers": [{
                "id": "simple-tiles",
                "type": "raster",
                "source": "raster-tiles",
            }]
        },
        center: [0, 0],
        zoom: 2
    });
    map.addControl(new mapboxgl.NavigationControl(), 'top-left');
    map.addControl(new mapboxgl.supermap.LogoControl(), 'bottom-right');
    featureService = new mapboxgl.supermap.FeatureService(dataUrl);

    var sourceFeatures = {},
        addPointFeaturesData = {
            "type": "FeatureCollection",
            "features": []
        };

    map.loadImage('../img/marker-icon.png', function (error, image) {
        if (error) throw error;
        map.addImage('positionPoint', image);
    });

    initFeature();

    function initFeature() {

        var polygon = {
            "type": "Polygon",
            "coordinates": [[[118, 20], [120, 20], [120, 50], [-120, 50], [118, 20]]]
        };
        var getFeatureParams = new SuperMap.GetFeaturesByGeometryParameters({
            toIndex: -1,
            datasetNames: ["World:Capitals"],
            geometry: polygon,
            spatialQueryMode: "INTERSECT"
        });
        featureService.getFeaturesByGeometry(getFeatureParams, function (serviceResult) {
            sourceFeatures = serviceResult.result.features;

            if (!map.getSource("queryDatas")) {
                map.addSource("queryDatas", {
                    "type": "geojson",
                    "data": sourceFeatures
                });
            } else {
                map.getSource("queryDatas").setData(sourceFeatures);
            }
            map.addLayer({
                "id": "queryDatasLayer",
                "type": "symbol",
                "source": "queryDatas",
                "layout": {
                    "icon-image": "positionPoint",
                    "icon-size": 0.8,
                    "icon-offset": [0, -15] //设置偏移量
                },
            });

            var popup = new mapboxgl.Popup({
                anchor: 'bottom',
                closeButton: false,
                offset: {
                    'bottom': [0, -20],
                }
            });
            map.on('mousemove', "queryDatasLayer", function (e) {
                popup.setLngLat(e.lngLat).setHTML(e.features[0].properties.CAPITAL).addTo(map);
                map.getCanvas().style.cursor = 'pointer';

            });
            map.on('mouseout', "queryDatasLayer", function () {
                map.getCanvas().style.cursor = '';
                popup.remove();
            })

        });
    }

    //添加地物
    function addMarker() {
        closeClearListener();
        widgets.alert.clearAlert();
        addPointFeaturesData.features = [];
        var xmax = 120, xmin = 100, ymax = 50, ymin = 20;
        var addPoint = [Math.floor(Math.random() * (xmax - xmin + 1) + xmin), Math.floor(Math.random() * (ymax - ymin + 1) + ymin)];
        pointFeature = {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": addPoint
            },
            "properties": {POP: 1, CAPITAL: 'test'}
        };

        addPointFeaturesData.features.push(pointFeature);

        if (!map.getLayer("addPoint")) {
            map.addSource("addPointsSource", {
                "type": "geojson",
                "data": addPointFeaturesData
            });
            map.addLayer({
                "id": "addPoint",
                "type": "circle",
                "source": "addPointsSource",
                'paint': {
                    'circle-radius': 8,
                    'circle-color': 'rgba(255, 0, 0, 0.2)',
                    "circle-stroke-color": 'red',
                    "circle-stroke-width": 2,
//                    "circle-translate": [0, 13] //设置偏移量
                },
            });
        } else {
            map.getSource("addPointsSource").setData(addPointFeaturesData);
        }

        map.flyTo({
            center: addPoint,
            zoom: 5,
            bearing: 0,
            speed: 1.2,
            curve: 1,
            easing: function (t) {
                return t;
            }
        });
    }

    function revocationMarker() {
        closeClearListener();
        if (pointFeature) {
            addPointFeaturesData.features = [];
            map.getSource("addPointsSource").setData(addPointFeaturesData);
            pointFeature = null;
        } else {
            widgets.alert.showAlert(resources.msg_noRevocableMarker, false);
        }
    }

    //提交添加
    function commit() {
        closeClearListener();
        widgets.alert.clearAlert();
        if (!pointFeature) {
            widgets.alert.showAlert(resources.msg_noMarkerToSubmit, false);
            return;
        }
        //更新添加的source
        var addFeatureParams = new SuperMap.EditFeaturesParameters({
            features: pointFeature,
            dataSourceName: "World",
            dataSetName: "Capitals",
            editType: "add",
            returnContent: true
        });
        featureService.editFeatures(addFeatureParams, function (serviceResult) {
            if (serviceResult.result.succeed) {
                addPointFeaturesData.features = [];
                map.getSource("addPointsSource").setData(addPointFeaturesData);
                pointFeature = null;
                map.removeLayer("queryDatasLayer");
                widgets.alert.showAlert(resources.msg_submitSuccess, true);
                pointFeature = null;
                initFeature();
            }
        });
    }


    var popup = new mapboxgl.Popup({
        anchor: 'left',
        closeButton: false,
        offset: {'left': [10, 10]}
    });

    function clearLayer() {
        closeClearListener();
        widgets.alert.clearAlert();
        map.on('mousemove', mousemoveListener);
        map.on('click', 'queryDatasLayer', dataClickListener);
    }

    function mousemoveListener(e) {
        popup.setLngLat(e.lngLat).setHTML(resources.msg_selectDeletePoint).addTo(map);
    }

    function dataClickListener(e) {

        var deleteParams = new SuperMap.EditFeaturesParameters({
            dataSourceName: "World",
            dataSetName: "Capitals",
            IDs: [e.features[0].properties.ID],
            editType: "delete"
        });

        featureService.editFeatures(deleteParams, function (serviceResult) {
            if (serviceResult.result.succeed) {
                map.removeLayer("queryDatasLayer");
                //重载数据默认集
                initFeature();
                map.getCanvas().style.cursor = '';
                closeClearListener();
                widgets.alert.showAlert(resources.text_deleteSuccess, true);
            } else {
                widgets.alert.showAlert(resources.msg_deleteFailed, false);
            }
        });
    }

    function closeClearListener() {
        popup.remove();
        map.off('mousemove', mousemoveListener);
        map.off('click', 'queryDatasLayer', dataClickListener);
    }

</script>
</body>
</html>